set more off
use "C:\Users\mjoshi2\Box\MJ Work From Home\ISQ Revisions\Final Submission\2019-05-09 ISA Systemic Peace Data.dta", replace
log using "C:\Users\mjoshi2\Box\MJ Work From Home\ISQ Revisions\Final Submission\2019-05-09 log file.smcl", replace
//rounding to integer
//gen agg_imprate_lag2 = round(agg_imprate_lag)
//sort ccode year
//by ccode: gen pkoaveragelag = pkoave_total_1000[_n-1]
*********XTSET DATA*********
 tsset ccode year
 **************************
label variable  sumconfv413 "Number of Active dyads"
label variable sumconfv413_lag "Number of Active dyad t-1"
label variable  pamrate_nonCPA_PAlag1 "CPA rate x Non-CPA t-1"
label variable  pamrate_Gov_v_lag1 "CPA rate x Government Victory t-1"
label variable  pamrate_Reb_v_lag1 "CPA rate x Rebel Victory t-1"
label variable  nonCPA_PALag "Non-CPA t-1"
label variable  reb_viclag  "Rebel victory t-1"
label variable  govt_viclag1 "Govt. victory t-1"
label variable  agg_imprate_lag "CPA implementation ratet-1"
label variable  bdbest_cumlag  "Cumulative deaths (best est.) t-1"
label variable  cum_peace_year "Years since last termination"
label variable firstyear "First year"  
label variable secondyear "Second year"
label variable  thirdyear "Third year"
label variable  fourth "Fourth year"
label variable  fifth  "Fifth year"
label variable  govonlyv413m  "Government conflict"
label variable  lninfantmort_lag "Log infant mortality rate t-1"
label variable  lnlagwdi_pop  "Log total population t-1”
label variable  lnwdi_gdppccon_lag  "Log GDP per capita (2005) t-1"
label variable  ex_constrain5_7_lag  "Executive constrain t-1"
label variable  polity2_lag "Polity2 t-1"
label variable  pkoaveragelag "UN deployment x 1,000 t-1"
label variable military_lag "External military support t-1"


//Descriptive Stat Table 1 
sum  sumconfv413 L.sumconfv413 pamrate_nonCPA_PAlag1 pamrate_Gov_v_lag1 pamrate_Reb_v_lag1 nonCPA_PALag reb_viclag govt_viclag1 agg_imprate_lag bdbest_cumlag cum_peace_year firstyear secondyear thirdyear fourth fifth  govonlyv413m  lninfantmort_lag  lnlagwdi_pop  lnwdi_gdppccon_lag  ex_constrain5_7_lag  polity2_lag pkoaveragelag military_lag if mj_conflictid!=. &  sumconfv413!=.

//Pairwise Correlation Table 2
pwcorr sumconfv413 L.sumconfv413 agg_imprate_lag nonCPA_PALag govt_viclag1 reb_viclag  pamrate_nonCPA_PAlag1 pamrate_Gov_v_lag1 pamrate_Reb_v_lag1 bdbest_cumlag cum_peace_year firstyear secondyear thirdyear fourth fifth  govonlyv413m lninfantmort_lag  lnlagwdi_pop  lnwdi_gdppccon_lag  ex_constrain5_7_lag polity2_lag pkoaveragelag military_lag if mj_conflictid!=. &  sumconfv413!=.

//Table 2
//Mode l - All terminations and control for year and govt conflict
xtreg sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  pkoaveragelag  firstyear secondyear thirdyear fourth fifth if mj_conflictid!=.,fe
collin sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  pkoaveragelag  firstyear secondyear thirdyear fourth fifth if mj_conflictid!=.

//Model 2 - includes ex constrains and peacekeeing, population, gdp percapita
 xtreg sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  lnlagwdi_pop  lnwdi_gdppccon_lag firstyear secondyear thirdyear fourth fifth  ex_constrain5_7_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  lnlagwdi_pop  lnwdi_gdppccon_lag firstyear secondyear thirdyear fourth fifth  ex_constrain5_7_lag pkoaveragelag if mj_conflictid!=.,  

 //Model 3 replace gdp per capita with infant mortality
xtreg sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  lnlagwdi_pop  lninfantmort_lag  firstyear secondyear thirdyear fourth fifth  ex_constrain5_7_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  lnlagwdi_pop  lninfantmort_lag  firstyear secondyear thirdyear fourth fifth  ex_constrain5_7_lag pkoaveragelag if mj_conflictid!=.,  

//Model 4 replace ex-constraint with polity2
xtreg sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  lnlagwdi_pop  lninfantmort_lag  firstyear secondyear thirdyear fourth fifth  polity2_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  lnlagwdi_pop  lninfantmort_lag  firstyear secondyear thirdyear fourth fifth  polity2_lag pkoaveragelag if mj_conflictid!=.,  

//Model 5
xtreg sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=.,  

//Figure 2
margins, at (agg_imprate_lag =(1(5)100))
marginsplot

//Table 3
//Table 3 Interaction Effects and additional models
//Model 6 non cpa 
xtreg sumconfv413 agg_imprate_lag pamrate_nonCPA_PAlag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag pamrate_nonCPA_PAlag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=.,  

//Model 7 gov victory
xtreg sumconfv413 agg_imprate_lag  pamrate_Gov_v_lag1   reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag  pamrate_Gov_v_lag1   reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=.,  

//Model 8 rebel victory
xtreg sumconfv413 agg_imprate_lag pamrate_Reb_v reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag pamrate_Reb_v reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=.,  

//Model 9 external military
xtreg sumconfv413 agg_imprate_lag military_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag military_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=.,  

//Model 10 lagged dependent variable
xtreg sumconfv413 agg_imprate_lag sumconfv413_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe
collin sumconfv413 agg_imprate_lag sumconfv413_lag reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=.,  

//Data for Table 4
xtreg sumconfv413 agg_imprate_lag  pamrate_Gov_v_lag1   reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe


//Margin data for Table 4
xtreg sumconfv413 agg_imprate_lag  pamrate_Gov_v_lag1   reb_viclag govt_viclag1 nonCPA_PALag govonlyv413m  bdbest_cumlag lnlagwdi_pop  lninfantmort_lag  cum_peace_year  polity2_lag pkoaveragelag if mj_conflictid!=., fe
 //10
 margins, at(agg_imprate_lag=10 govt_viclag1=0 pamrate_Gov_v_lag1==0)
 margins, at(agg_imprate_lag=10 govt_viclag1=1 pamrate_Gov_v_lag1==10)

 //20
 margins, at(agg_imprate_lag=20 govt_viclag1=0 pamrate_Gov_v_lag1==0)
margins, at(agg_imprate_lag=20 govt_viclag1=1 pamrate_Gov_v_lag1==20)

//30
margins, at(agg_imprate_lag=30 govt_viclag1=0 pamrate_Gov_v_lag1==0)
margins, at(agg_imprate_lag=30 govt_viclag1=1 pamrate_Gov_v_lag1==30)

//40
margins, at(agg_imprate_lag=40 govt_viclag1=0 pamrate_Gov_v_lag1==0)
margins, at(agg_imprate_lag=40 govt_viclag1=1 pamrate_Gov_v_lag1==40)

log close
